package project.android;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.database.Cursor;


public class Synchronized{
	
	public static String username ;
	public static String password;

	private static void getAccountInfo(DatabaseConnection db)throws Exception{
		
		db.connectDatabase("DatabaseName","Authentication");
		Cursor c = db.selectAll();
		c.moveToFirst();	
		username = (c.getString(c.getColumnIndex("USERNAME")));
		password = (c.getString(c.getColumnIndex("PASSWORD")));
		db.close();
		
	}
	
	public static void syncTasks(DatabaseConnection db)throws Exception{
		
		//pull username and password from database
		getAccountInfo(db);
		String toBeSend = username+"*";
		toBeSend += password+"*";
		
		//pull unsynchronized tasks from database
		db.connectDatabase("DatabaseName","Tasks");
		Cursor c = db.select("SYNC","0");
		do {
    		
    		toBeSend += (c.getString(c.getColumnIndex("NAME")))+"*";
			toBeSend += (c.getString(c.getColumnIndex("DESCRIPTION")))+"*";
			toBeSend += (c.getString(c.getColumnIndex("DEADLINE")))+"*";
			toBeSend += (c.getString(c.getColumnIndex("ASSIGNEDBY")))+"*";
			toBeSend += (c.getString(c.getColumnIndex("GROUPNAME")))+"*";
			toBeSend += (c.getString(c.getColumnIndex("GROUPID")))+"*";
			toBeSend += Integer.toString(c.getInt(c.getColumnIndex("FINISHED")))+"*";
			toBeSend += (c.getString(c.getColumnIndex("GUID")))+"*";
			toBeSend += Integer.toString(c.getInt(c.getColumnIndex("DELETED")))+"*";
			
		}while(c.moveToNext());

		//connect to server for synchronization
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    	nameValuePairs.add(new BasicNameValuePair(toBeSend, null));
    	
    	String Data = ServerConnection.postData(nameValuePairs,"http://192.168.0.1:8080/tasks/synctasks");
    	
		//update database for newly synchronized tasks
    	String toBeInserted;
    	c.moveToFirst();
    	do {

    		if(c.getInt(c.getColumnIndex("DELETED")) == 1)
				db.delete(c.getInt(c.getColumnIndex("ID")));
    		else{
    			
	    		toBeInserted =new String();
	    	
	    		toBeInserted += (c.getString(c.getColumnIndex("NAME")))+",";
				toBeInserted += (c.getString(c.getColumnIndex("DESCRIPTION")))+",";
				toBeInserted += (c.getString(c.getColumnIndex("DEADLINE")))+",";
				toBeInserted += (c.getString(c.getColumnIndex("ASSIGNEDBY")))+",";
				toBeInserted += (c.getString(c.getColumnIndex("GROUPNAME")))+",";
				toBeInserted += (c.getString(c.getColumnIndex("GROUPID")))+",";
				toBeInserted += Integer.toString((c.getInt(c.getColumnIndex("FINISHED"))))+",";
				toBeInserted += "1,";
				toBeInserted += (c.getString(c.getColumnIndex("GUID")))+",";
				toBeInserted += Integer.toString((c.getInt(c.getColumnIndex("DELETED"))));
			
				db.update(c.getInt(c.getColumnIndex("ID")), toBeInserted);
    		
    		}
		
		}while(c.moveToNext());
    
		
		//if I have received some task info from server
    	if( Data.length() > 4 ){
    		
    		String[] tasks = Data.substring(1).split("=");
    		
    		int numberoftasks = tasks.length-1;
    		
    		for(int i = 0;i<numberoftasks;i++){
    			
    			String[] temp = tasks[i].split("~");
    			String name= temp[0];
    			String description = temp[1];
    			String deadline = temp[2];
    			String assignedby = temp[3];
    			String groupname= temp[4];
    			String groupid= temp[5];
    			int finished= Integer.parseInt(temp[6]);
    			String guid = temp[7];;
    			int deleted = Integer.parseInt(temp[8]);
    			c =db.select("GUID",guid);
    			toBeInserted = name+","+description+","+deadline+","+
    					assignedby+","+groupname+","+groupid+","+finished+",1,"
    					+guid+","+deleted;
    			
    			if(c.getCount() != 0 ){
    				if(deleted == 1)
    					db.delete(c.getInt(c.getColumnIndex("ID")));
    				else
    					db.update(c.getInt(c.getColumnIndex("ID")), toBeInserted);	
    			}
    			else if(deleted == 0)
    				db.insert(toBeInserted);
    			
    			
    		}
    		
    	}
    	db.close();
    	
	}

	public static String[] syncGroups(DatabaseConnection db , String info , String type)throws Exception{
		
		getAccountInfo(db);
		String toBeSend = username+"*";
		toBeSend += password+"*";
		toBeSend += info;
		String url = "http://192.168.0.1:8080/" + type;
		
		//connect to server for synchronization
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    	nameValuePairs.add(new BasicNameValuePair(toBeSend, null));
    	
    
    	String received = 	ServerConnection.postData(nameValuePairs,url);
    	
    	if(type == "groups/addGroup" || type == "tasks/updatetask" || type == "tasks/deletetask"
    			|| type == "groups/leavegroupandroid")
    		return null;
    	
    	String[] parsed = null;
    	
    	if( received.length() > 0 )	{
    		received = received.substring(1);
    		parsed = received.split("=");
    	}
    	return parsed;
    	
	}
	
}
